#ifndef _DANIEL_KERR_GLMATERIAL_H
#define _DANIEL_KERR_GLMATERIAL_H

#include "driver.h"

class material_C
{
	public:
		void apply()
		{
			glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient);
			glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
			glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emission);
			glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
			glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
		}

		void applyRGB(GLfloat r,GLfloat g,GLfloat b,GLfloat alpha)
		{
			setAmbient(0.3*r,0.3*g,0.3*b,alpha);
			setDiffuse(0.6*r,0.6*g,0.6*b,alpha);
			setSpecular((0.2*r)+0.6,(0.2*g)+0.6,(0.2*b)+0.6,alpha);
			setShininess(32.0);
			apply();
		}

		void applyTexture()
		{
			setAmbient(0.3,0.3,0.3,1.0);
			setDiffuse(0.6,0.6,0.6,1.0);
			setSpecular(0.8,0.8,0.8,1.0);
			setShininess(32.0);
			apply();
		}

		void applyEdge(GLfloat max,int x,GLfloat alpha)
		{applyRGB(x/max,0,(max-x)/max,alpha);}

		void applyRGB_glow(GLfloat r,GLfloat g,GLfloat b,GLfloat gr,GLfloat gg,GLfloat gb,GLfloat alpha)
		{setEmission(gr,gg,gb,alpha); applyRGB(r,g,b,alpha);}

		void enableBlend()
		{glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);}

		void disableBlend()
		{glDisable(GL_BLEND);}

	private:
		GLfloat ambient[4];
		GLfloat diffuse[4];
		GLfloat emission[4];
		GLfloat specular[4];
		GLfloat shininess;

		void setAmbient(GLfloat r,GLfloat g,GLfloat b,GLfloat alpha)
		{ambient[0]=r; ambient[1]=g; ambient[2]=b; ambient[3]=alpha;}
		
		void setDiffuse(GLfloat r,GLfloat g,GLfloat b,GLfloat alpha)
		{diffuse[0]=r; diffuse[1]=g; diffuse[2]=b; diffuse[3]=alpha;}

		void setEmission(GLfloat r,GLfloat g,GLfloat b,GLfloat alpha)
		{emission[0]=r; emission[1]=g; emission[2]=b; emission[3]=alpha;}
		
		void setSpecular(GLfloat r,GLfloat g,GLfloat b,GLfloat alpha)
		{specular[0]=r; specular[1]=g; specular[2]=b; specular[3]=alpha;}
		
		void setShininess(GLfloat alpha)
		{shininess=alpha;}
};

#endif